package utils

import java.util
import java.util.Calendar

import model.request.{WXOrderQueryRequest, WXPayCloseRequest, WXPayRefundRequest, WXPayRequest, WXRefundQueryRequest, WXSignkeyRequest}
import model.response.{QueryResponse, RefundResponse, WXOrderQueryResponse, WXPayRefundResponse, WXPayResponse}

object BuildDataUtil {

  /**
   * 构造下单map
   *
   * @param req
   * @return
   */
  def buildMap(req: WXPayRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("appid", req.appid)
    map.put("attach", req.attach)
    map.put("body", req.body)
    map.put("mch_id", req.mchId)
    map.put("nonce_str", req.nonceStr)
    map.put("notify_url", req.notifyUrl)
    map.put("out_trade_no", req.outTradeNo)
    map.put("spbill_create_ip", req.spbillCreateIp)
    map.put("time_expire", req.timeExpire)
    map.put("total_fee", String.valueOf(req.totalFee))
    map.put("trade_type", req.tradeType)
    map.put("sign", req.sign)
    map
  }

  /**
   * 构造查询map
   *
   * @param req
   * @return
   */
  def buildMap(req: WXOrderQueryRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("appid", req.appid)
    map.put("mch_id", req.mchId)
    map.put("nonce_str", req.nonceStr)
    map.put("sign", req.sign)
    map.put("sign_type", req.signType)
    map.put("out_trade_no", req.outTradeNo)
    map
  }

  /**
   * 构造关单参数
   *
   * @param req
   * @return
   */
  def buildMap(req: WXPayCloseRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("appid", req.appid)
    map.put("mch_id", req.mchId)
    map.put("sign", req.sign)
    map.put("out_trade_no", req.outTradeNo)
    map
  }

  /**
   * 构造退款map
   *
   * @param req
   * @return
   */
  def buildMap(req: WXPayRefundRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("appid", req.appid)
    map.put("mch_id", req.mchId)
    map.put("nonce_str", req.nonceStr)
    map.put("notify_url", req.notifyUrl)
    map.put("out_refund_no", req.outRefundNo)
    map.put("out_trade_no", req.outTradeNo)
    map.put("refund_fee", String.valueOf(req.refundFee))
    map.put("total_fee", String.valueOf(req.totalFee))
    map
  }

  /**
   * 构造退款查询
   *
   * @param req
   * @return
   */
  def buildMap(req: WXRefundQueryRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("appid", req.appid)
    map.put("mch_id", req.mchId)
    map.put("nonce_str", req.nonceStr)
    map.put("out_refund_no", req.outRefundNo)
    map
  }


  /**
   * 构造获取沙箱signkey的map
   *
   * @param req
   * @return
   */
  def buildMap(req: WXSignkeyRequest): util.Map[String, String] = {
    val map = new util.HashMap[String, String]
    map.put("mch_id", req.mchId)
    map.put("nonce_str", req.nonceStr)
    map
  }

  def buildMap(wxPayResp: WXPayResponse, signKey: String): util.Map[String, String] = {
    val calendar = Calendar.getInstance
    val map = new util.HashMap[String, String]
    map.put("appid", wxPayResp.appid)
    map.put("noncestr", wxPayResp.nonceStr)
    map.put("package", "Sign=WXPay")
    map.put("partnerid", wxPayResp.mchId)
    map.put("prepayid", wxPayResp.prepayId)
    map.put("timestamp", String.valueOf(System.currentTimeMillis / 1000L))
    map.put("sign", SignatureUtil.sign(map, signKey))
    map
  }

  /**
   * 查询结果封装
   *
   * @param resp
   * @return
   */
  def buildQueryResponse(resp: WXOrderQueryResponse): QueryResponse = {
    val queryResp = new QueryResponse
    queryResp.cashFee = resp.cashFee
    queryResp.couponFee = resp.couponFee
    queryResp.openid = resp.openid
    queryResp.orderId = resp.outTradeNo
    queryResp.totalFee = resp.totalFee
    queryResp.bankType = resp.bankType
    queryResp.feeType = resp.feeType
    queryResp.tradeState = resp.tradeState
    queryResp
  }

  /**
   * 退款结果封装
   *
   * @param resp
   * @return
   */
  def buildRefundResponse(resp: WXPayRefundResponse): RefundResponse = {
    val refundResp = new RefundResponse
    refundResp.orderId = resp.outTradeNo
    refundResp.orderAmount = Long.unbox(resp.totalFee)
    refundResp.outTradeNo = resp.transactionId
    refundResp.refundId = resp.outRefundNo
    refundResp.outRefundNo = resp.refundId
    refundResp
  }

  /**
   * 支付预下单结果封装
   *
   * @param
   * @return
   */
  /*private PayResponse buildPrePayResponse(WXPayResponse response) {
      //跳转的url
      PayResponse payResponse = new PayResponse();
      payResponse.setCode(response.getResultCode());
      payResponse.setMsg(response.getReturnMsg());
      if(StringUtils.isNotBlank(response.getMwebUrl())){
          payResponse.setMwebUrl(response.getMwebUrl());
      }
      return payResponse;
  }*/

}
